// pages/mine_code/index.js
const api = require('../../../utils/api').apis;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone: '',
    code: '',
    num: "",
    numis: false,
    one: "",
    two: "",
    three:"",
    four:"",
    value: '',
    code:[],
    isFocus:true,
  },
  bindblur(){
    this.setData({
      isFocus:false,
  })
  },

  // 验证码
  formBindsubmit(e) {
    console.log(e.detail.value)
    var arr = e.detail.value
    var code = arr.code + arr.code1 + arr.code2 + arr.code3
    // var code = '"'+arr[0]+''+arr[1]+''+arr[2]+''+arr[3]+'"'
    // console.log(code)
    this.setData({
      code: code
    })
    this.getuserCodeRegister()
  },
  focus(){
    this.setData({
      isFocus:true,
    })
  },
  bindfocus(e) {
    var value = e.detail.value.toString()
    this.setData({
      one: value.substr(0, 1),
      two: value.substr(1, 1),
      three: value.substr(2, 1),
      four: value.substr(3),
      code:value.substr(0, 1)+value.substr(1, 1)+value.substr(2, 1)+value.substr(3),
      isFocus:true
    })
    // console.log(one,two.three,four)

  },
  getuserCodeRegister() {
    console.log(this.data.code.substr(0,4))
    api.userUpdateUserPhone({
      phone: this.data.phone,
      code: this.data.code.substr(0,4),
    }).then(res => {
      console.log(res)
      if (res.success) {
        wx.showToast({
          title: '更改手机号成功',
          icon: 'none'
        })
        wx.setStorageSync('userNumber', this.data.phone)
        wx.setStorageSync('Xd_Token',res.data)
        // wx.login({
        //   success: res => {
        //     if (res.code) {
        //       wx.showLoading({
        //         title: '加载中',
        //       })
        //       console.log(res)
        //       var code = res.code
        //       api.getLogin({
        //         code: code
        //       }).then(res => {

        //         // wx.setStorageSync('openId', res.data.userBO.appletOpenId)

        //         var token = res.data.token
        //         wx.setStorageSync('Xd_Token', token)
        //       })
        //     }
        //   }
        //   })
        setTimeout(function () {
          wx.navigateBack({
            delta: 2, // 返回上一级页面。
            success: function () {
              console.log('成功！')
            }
          })
        }, 1000)
      }else{
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
      }
    })
  },
  // 重新获取验证码
  setnum(res) {
    if (res) {
      // 计时开始 
      var times = 60
      var timer = null;
      var _this = this
      timer = setInterval(function () {
        times--;
        if (times <= 0) {
          clearInterval(timer);
          times = 60;
          _this.setData({
            numis: false,
            num: ''
          })
        } else {
          _this.setData({
            numis: true,
            num: times + 's'
          })
        }
      }, 1000)
    }
  },
  // 获取验证api
  getUserNumbercode() {
    api.getUserNumbercode({
      phone: this.data.phone,
      type: 'amendPhone'
    }).then(res => {
      console.log(res)
      if (res.success) {
        this.setData({
          numis: true
        })
        this.setnum(true)
      }
    })
  },
  // 重新获取验证码点击事件
  giancode() {
    if (!this.data.numis) {
      console.log('验证码已发送')
      this.getUserNumbercode()
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    var phone = options.phone
    // var phone = '12345678910'
    let list = phone.substr(0, 3) + '****' + phone.substr(7)
    console.log(phone)

    this.setData({
      phone: phone,
      list: list,
    })
    this.setnum(true)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})